/**
 * Copyright (c) 2010, G&T Services Italy
 * All rights reserved.

 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 *  - Neither the name of G&T Services Italy nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * @author Cristian Giustini
 * @category jQuery Scroll Plugin
 * @description gtScroller allows you to perform an automatic slide simply providing an
 * unordered list (<ul>) and some css to style it a bit.
 * [NOTE]: gtScroller plugin doesn't do any kind of css modification, you need to do by yourself.
 *
 * @example $("ul#list").scroller({
 *    height: '200px',
 *    time: '1000'
 * });
 *
 */

(function($) {
      $.fn.scroller = function( options ){

            var settings = {
                  height: '140px', // The height of the container
                  time: '4000', // The duration time of each slide
                  animation: 'slideDown', // The type of animation (can be slideDown,fadeIn or show)
                  stopAnimationOnHover: false,
                  backgroundColorOnHover: false
            };
            /**
             * Do not touch under unless you know what are you doing.
             */
            if ( options ){
                  $.extend(settings, options);
            }

            function prepareCss(obj)
            {
                  $(obj).css({
                        'overflow' : 'hidden',
                        'height' : settings.height
                  });
            }

            function startTimeout(obj)
            {
                  tout = setTimeout(function() {
                        var lastElement = $(obj).find("li:last");

                        $(lastElement).detach().prependTo(obj).hide();

                        if (settings.animation == 'fadeIn')
                              $(lastElement).fadeIn("slow");
                        else if (settings.animation == 'show')
                              $(lastElement).show("slow");
                        else
                              $(lastElement).slideDown("slow");

                        startTimeout(obj);
                  }, settings.time);
            }

            function stopTimeout()
            {
                  if (tout)
                        clearTimeout(tout);
            }

            function changeBackgroundColor(obj)
            {
                  $(obj).find("li").each(function() {
                        var currentColor = $(this).css('background-color');
                        $(this).mouseover(function() {
                              $(this).stop().animate({
                                    backgroundColor: settings.backgroundColorOnHover
                              })
                        }).mouseout(function() {
                              $(this).stop().animate({
                                    backgroundColor: currentColor
                              })
                        });
                  });
            }

            return this.each(function() {
                  var obj = $(this);
                  prepareCss(obj);

                  if (settings.backgroundColorOnHover)
                        changeBackgroundColor(obj);

                  if (settings.stopAnimationOnHover)
                  {
                        $(obj).mouseover(function() {
                              stopTimeout(obj);
                        }).mouseout(function() {
                              startTimeout(obj);
                        });
                  }
                  startTimeout(obj);
            });
      }
})(jQuery);